<?php

namespace App\Http\Controllers;

use App\Http\Requests\UserAddressRequest;
use App\Models\UserAddress;
use Illuminate\Http\Request;

class UserAddressesController extends Controller
{
	/**
	 * 我的收货地址
	 * @Function index
	 *
	 * @param Request $request
	 *
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * @Author   tegic
	 * @Time     2018/6/22 14:24
	 */
	public function index(Request $request)
	{
		return view('user_addresses.index', [
			'addresses' => $request->user()->addresses,
		]);
	}
	
	/**
	 * 创建收货地址
	 * @Function create
	 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
	 * @Author   tegic
	 * @Time     2018/6/22 14:25
	 */
	public function create()
	{
		return view('user_addresses.create_and_edit', [ 'address' => new UserAddress() ]);
	}
	
	/**
	 * 提交创建
	 * @Function store
	 * @param UserAddressRequest $request
	 *
	 * @return \Illuminate\Http\RedirectResponse
	 * @Author   tegic
	 * @Time     2018/6/22 14:43
	 */
	public function store(UserAddressRequest $request)
	{
		$request->user()->addresses()->create($request->only([
			'province',
			'city',
			'district',
			'address',
			'zip',
			'contact_name',
			'contact_phone',
		]));
		
		return redirect()->route('user_addresses.index');
	}
}
